﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace BanditGame
{
    public class Bandit
    {
        private int _moveX = 0;
        private int _moveY = 0;

        /// <summary>
        /// Players horizontal position
        /// </summary>
        public int PositionX { get; set; }

        /// <summary>
        /// Players vertical position
        /// </summary>
        public int PositionY { get; set; }


        // * Methods for moving on gamefield, 
        //   called when keyboard arrows are pressed
        public void moveRight()
        {
            _moveX = 1;
            _moveY = 0;
            //updatePosition(this.moveX, this.moveY);
        }
        public void moveLeft()
        {
            _moveX = -1;
            _moveY = 0;
            //updatePosition(this.moveX, this.moveY);
        }
        public void moveUp()
        {
            _moveX = 0;
            _moveY = -1;
            //updatePosition(this.moveX, this.moveY);
        }

        public void moveDown()
        {
            _moveX = 0;
            _moveY = 1;
            //updatePosition(this.moveX, this.moveY);
        }

        // Updates position of bandit
        public void updatePosition()
        {
            PositionX += _moveX;
            PositionY += _moveY;
        }

        //Collision Checking
        public bool CheckForCollision(Level currentLevel)
        {
            
            bool collision = false;

            //Checking for collisions with borders of canvas (in case someone forget make wall)
            if ((PositionX + _moveX) < 0)
            {
                _moveX = 0;   /* left border */
                return collision;
            }
            if ((PositionY + _moveY) < 0)
            {
                _moveY = 0;   /* top border */
                return collision;
            }

            /******************************************************************************************************************************************************************************************************************/
            if ((PositionX + _moveX) > currentLevel.SizeX)//Defaults.MaxGameFieldWidth) // Settings.gameFieldWidth)
            {
                //this.positionX = Settings.gameFieldWidth;    /* bottom border */
                _moveX = 0; /* left border */
                return collision;
            }
            /******************************************************************************************************************************************************************************************************************/
            if ((this.PositionY + this._moveY) > currentLevel.SizeY)//Defaults.MaxGameFieldHeight) //Settings.gameFieldHeight)
            {
                //this.positionY = Settings.gameFieldHeight;   /* right border */
                _moveY = 0;   /* top border */
                return collision;
            }

            Cell nextCell =
                currentLevel.CellList.Where
                (x => x.PositionX == (this.PositionX + _moveX)).Where(y => y.PositionY == 
                    (this.PositionY + this._moveY)).FirstOrDefault();

            //Checking collisions with wall
            if (nextCell.CellType == '#')
            {
                collision = true;
            }

            //Checking collisions with question
            if (nextCell.CellType == '?')
            {
                QuestionWindows questWin = new QuestionWindows();
                questWin.Show();
            }


            return collision;

        }
    }
}
